package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f=new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        String queue= UUID.randomUUID().toString();
        //创建一个具有生成名称的、非持久的、独占的、自动删除队列
        c.queueDeclare(queue, false, true, true, null);

        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        //对于fanout类型交换机，第三个参数无效
        c.queueBind(queue, "logs", "");


        DeliverCallback callback=new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String s = new String(message.getBody());
                System.out.println("收到: "+s);
            }
        };

        CancelCallback cancel=new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };

        c.basicConsume(queue, true,callback,cancel);

    }
}
